<?php
/**
 * Created by PhpStorm.
 * User: songs
 * Date: 2019/10/12
 * Time: 10:13
 */

namespace App\admin\TableView;


use App\admin\TableView\BaseClass\BaseViewList;
use App\Models\Admin\ApilistModel;

class ApilistView extends BaseViewList
{

    public function get_model()
    {

        return new ApilistModel();
    }

    public $topTitle = '接口';
    public $PageSize = 10;

    public $breadcrumb = '<li><a href="/mcadmin">Home</a></li>
        <li><a href="javascript:;">接口</a></li>
        ';

    public $html = '';


    //  public $show_add = false; ///显示添加按钮

    /**
     * 获取表格的唯一名字
     */
    public function getName()
    {
        return 'ApilistModel';
    }


    /**
     * 需要继承,然后定义每行需要显示的数据格式
     */
    public function Labels()
    {
        return array(
            //   BaseViewList::Button_checkbox=>[],
            'id' => [
                'width' => 40
            ],
            'name' => ['width' => 80],
            'url' => ['width' => 180],
            'api' => ['width' => 120],
            'call' => ['width' => 200],
            'auth' => ['width' => 70],
           /* 'count' => ['width' => 50,
                'sort' => 'true'],
            'success' => ['width' => 40,
                'sort' => 'true'],
            'fail' => ['width' => 40,
                'sort' => 'true'],*/
            'notes' => ['width' => 180],
          //  'average_time' => ['width' => 90, 'title' => '平均耗时',  'sort' => 'true'],
           // 'runtest' => ['width' => 100, 'title' => '测试状态'],
            'isrun' => ['width' => 60],
            BaseViewList::Button_control => [

                BaseViewList::Button_Edit,
                BaseViewList::Button_Delete,
                'apiinfoedit',

            ]
        );
    }

    /**
     * 对话框中的各个字段
     */
    public function dialog()
    {
        return array(
            array(
                'zd' => 'name'
            ),
            array(
                'zd' => 'api'
            ),
            array(
                'zd' => 'call'
            ),
            array(
                'zd' => 'auth',
                'type' => self::dialog_list,
                'data' => ApilistModel::getAuthArray()
            ),
            array(
                'zd' => 'isrun',
                'type' => self::dialog_list,
                'data' => [
                    1 => '启用',
                    0 => '关闭'
                ]
            ),
            array(
                'zd' => 'notes',
                'type' => self::dialog_textarea,
            ),
        );
    }

    /**
     * 方便继承,多一层过滤
     */
    public function ModelTohtml($key, $model)
    {
        if ($key == 'runtest') {
            return ApilistModel::getTest($model);
        }

        if($key=='url'){
            return '/api/app/call/'.$model->api;
        }

        if ($key == 'isrun') {
            return $this->ArrayToTitle($model->$key, [
                1 => '启用',
                0 => '关闭'
            ]);
        }
        if ($key == 'auth') {
            return $this->ArrayToTitle($model->$key, ApilistModel::getAuthArray());
        }
        if ($key == 'average_time') {
            if ($model->count == 0 || empty($model->count)) {
                return '无效';
            }
            return round($model->runtime / $model->count / 1000, 3);
        }

        return parent::ModelTohtml($key, $model);
    }


    /**
     * 返回查询的条件
     */
    public function find()
    {
        $find = $this->get_model()->where([
        ]);
        $word = $_GET['word'] ?? '';
        if ($word !== '') {
            $word = '%' . $word . '%';
            $find->whereRaw('name LIKE ? OR notes LIKE ?', [
                $word, $word
            ]);
        }
        $api = $_GET['api'] ?? '';
        if ($api !== '') {
            $api = '%' . $api . '%';
            $find->whereRaw('api LIKE ? OR `call` LIKE ?', [
                $api, $api
            ]);
        }

        $auth = $_GET['auth'] ?? '';
        if ($auth !== '') {
            $find->where([
                'auth'=>$auth
            ]);
        }
        $isrun = $_GET['isrun'] ?? '';
        if ($isrun !== '') {
            $find->where([
                'isrun'=>$isrun
            ]);
        }

        $find->orderBy('id', 'DESC');
        return $find;
    }

    /**
     * 顶部的选择区,参数,如果为空,则不显示选择器
     */
    public function getselectAttr()
    {

        ///例子
        $where = [
            'word' =>
                [
                    'title' => '名字和注释',
                    'inputtype' => self::htmlText,
                ],
            'api' =>
                [
                    'title' => '接口字符串和类名',
                    'inputtype' => self::htmlText,
                ],
            'auth' =>
                [
                    'title' => '授权',
                    'inputtype' => self::dropDownList,
                    'items' => ['' => '所有'] + ApilistModel::getAuthArray(),
                    'default' => ''
                ],
            'isrun' =>
                [
                    'title' => '是否启用',
                    'inputtype' => self::dropDownList,
                    'items' => ['' => '所有', 1 => '启用',
                        0 => '关闭'],
                    'default' => ''
                ]
        ];
        return $where;
    }






    public function val_control_apiinfoedit($model){
        return '<a target="_blank" class="edit btn  btn-info btn-xs" href="/mcadmin/apiedit?id='.$model->id.'">详细编辑</a>';
    }
















}